﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace 观察者模式
{
    //天气数据方法
    class WeatherData : Subject
    {
        private List<Observer> observersList = new List<Observer>();
        private float temperature;//温度
        private float humidity;//湿度
        private float pressure;//气压

        private List<float> forecastTemperatures; //未来几天的湿度


        //通知观察者
        public void notifyObservers()
        {
            foreach(var observers in observersList)
            {
                observers.update("");
            }
        }

        //注册观察者
        public void registerObserver(Observer observer)
        {
           
            this.observersList.Add(observer);
        }
         
        //移除观察者
        public void removeObserver(Observer observer)
        {
            this.observersList.Remove(observer);
        }

        //天气数据变更
        public void measurementsChanged()
        {
            notifyObservers();
        } 

        /// <summary>
        /// 设置天气数据
        /// </summary>
        /// <param name="temperature"></param>
        /// <param name="humidity"></param>
        /// <param name="pressure"></param>
        /// <param name="forecastTemperatures"></param>
        public void setMeasurements(float temperature, float humidity,float pressure, List<float> forecastTemperatures)
        {
            this.temperature = temperature;
            this.humidity = humidity;
            this.pressure = pressure;
            this.forecastTemperatures = forecastTemperatures;
            measurementsChanged();
        }

        /// <summary>
        /// 获取温度信息
        /// </summary>
        /// <returns></returns>
        public float getTemperature()
        {
            return temperature;
        }

        /// <summary>
        /// 获取湿度信息
        /// </summary>
        /// <returns></returns>
        public float getHumidity()
        {
            return humidity;
        }

        /// <summary>
        /// 获取气压信息
        /// </summary>
        /// <returns></returns>
        public float getPressure()
        {
            return pressure;
        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <returns></returns>
        public List<float> getForecastTemperatures()
        {
            return forecastTemperatures;
        }

    }
}
